package org.momo.rpc.constant;

import lombok.Getter;

import java.util.Objects;

@Getter
public enum SerializeKey {
    JSON("json", 0),
    HESSIAN("hessian", 1);

    private final String key;

    private final byte code;

    SerializeKey(String key, int code) {
        this.key = key;
        this.code = (byte) code;
    }

    public static String getSerializerKeyByCode(byte code) {
        for (SerializeKey serializeKey : values()) {
            if (serializeKey.getCode() == code) {
                return serializeKey.getKey();
            }
        }
        return null;
    }

    public static byte getSerializerCodeByKey(String key) {
        for (SerializeKey serializeKey : values()) {
            if (Objects.equals(serializeKey.getKey(), key)) {
                return serializeKey.getCode();
            }
        }
        return -1;
    }
}
